Skip to main content

Using Recipes to Set Values

Use recipes to change or read recipes values for a specific set of variables (recipe definition) on the controller at the same time.

You define the basic settings for recipes, such as location and format, in the Recipe Manager object. Insert any number of recipe definitions below this object.

You can save a recipe to a file or write directly from files to the PLC.

Recipes can be called via the CODESYS development interface, the visualization element, or the application program.

Using recipes on remote devices

If data source variables are involved and a data source exchange is configured, then variable values from recipes are automatically transferred to/from another controller. Reading and writing occurs synchronously. This means that all variables configured in a recipe definition are updated at the same time.

After reading or writing, you can use the call g_RecipeManager.LastError to check whether or not the transfer was successful (g_RecipeManager.LastError = 0).

Calling recipe commands in the CODESYS user interface

The development interface of CODESYS provides commands for generating recipes as well as reading/writing in online mode.

Calling recipe commands programmatically or in a visualization

At runtime, the recipe definition and the recipe commands can be called in the application program. To do this, you implement the process for writing, reading, or monitoring recipe definitions in your code by using the methods of the RecipeManCommands function block from the RecipeManagement library.

For more information, see: RecipeManCommands (FB)

In the visualization, the call of the recipe commands is implemented by using the input configuration (execute command) for a visualization element. By doing this, a visualization user can control the execution of recipe commands.

Tip

During the initialization process, the recipe management reads the values of the variables that are defined in the recipe definition. This operation takes place at the end of the initialization phase of the application. At this point, all initial values of the application variables are set. This is done to initialize missing values from recipe files correctly.

Special functionality for floating-point numbers

For variables of type REAL or LREAL, a distinction is made whether or not the value is exactly convertible.

  • If it is possible to convert the value exactly, then only the numerical value is saved in the recipe file.

  • If exact conversion is not possible, then a hexadecimal encoded string is written in the recipe file next to the numeric value. This should have the effect that also a non-convertible floating-point number returns the same value.

This feature can be disabled with the RECIPE_GENERATE_SIMPLE_STRINGREAL compiler define.

Tip

In the object properties of the parent application under Build, you can enter this RECIPE_GENERATE_SIMPLE_STRINGREAL compiler define in the Compiler Defines option. As a result, no hexadecimal encoded string is stored.

For more information, see: Dialog: Properties: Build

Creating a recipe

  1. In the device tree, select the Application object.

  2. Click Project → Add Object → Recipe Manager.

    The Recipe Manager is added to the device tree.

  3. In the device tree, click the Recipe Manager object.

  4. Click Project → Add Object → Recipe Definition.

    The recipe definition is added below the Recipe Manager.

  5. Open the editor of the recipe definition by double-clicking the object.

  6. Double-click the blank field below Variable. Specify the name of a variable to define a recipe for. You can use the Input Assistant for this by clicking the _cds_icon_button_threedots.png button).

  7. Click Recipes → Add a New Recipe and specify a name for the new recipe.

    A column with the new recipe name appears in the editor.

  8. Enter the variable value for this recipe in this field.

  9. Insert additional fields as needed.

  10. Select a variable value for the recipe and click Recipes→ Save Recipe. Select a location and file name.

    The recipe is saved in the format which was defined in the Recipe Manager.

Loading a recipe from a file

Requirement: A Recipe Manager is available in the application. In a recipe definition, there is a myRec recipe with variable values. A myRec.txt recipe file is located on the file system and contains the entries for this recipe.

Example of the recipe file:

PLC_PRG.bVar:=0
PLC_PRG.iVar:=2
PLC_PRG.dwVar:=35232
PLC_PRG.stVar:='first'
PLC_PRG.wstVar:='123443245'
  1. Double-click the Recipe Definition object in the device tree to open the tabular editor for the definition of the individual recipes.

    You see the myRec column with the current values for this recipe.

  2. Edit the myRec.txt file in an external text editor and replace the variable values with other values that you want to load into the recipe definition in CODESYS. Save the file.

  3. In the recipe definition, click the myRec column and in the context menu click Load Recipe.

    A dialog prompt notifies you about the possibly needing to perform an online change when logging in again. An online change is necessary when you change the current values of the recipe variables by loading the recipe.

  4. Click Yes to close the dialog and continue. Select the myRec.txt file from the file explorer for loading.

    The recipe values in the recipe definition are updated according to the values read in the file.

Tip

If you want to overwrite only individual recipe variables with new values, then remove the values for the other variables before loading to the recipe file. Entries without value definitions are not read, and therefore updating leaves these variables unchanged on the controller and in the project.

For values of the data type REAL/LREAL, the hexadecimal value is also written to the recipe file in some cases. This is necessary so that the exactly identical value is restored when converting back. In this case, you change the decimal value and delete the hexadecimal value.

Recipe management on the controller; memory usage

When you clear the Recipe management in the PLC option, the Recipe Manager and recipe definitions will not use any memory on the PLC.

If you select this option, then code is generated for the Recipe Manager and all recipe definitions, and this code is stored on the PLC. The size of the used memory primarily depends on the number pf recipes and their variables, as well as the data type of the variables. Whether or not the fields of the recipe definition are filled also has an effect. The memory usage of recipes cannot be calculated. It has to be determined by experimentation at the time it is needed. The following table merely provides some guiding principles.

Table 1. Memory usage for CODESYS V3 SP6 with a CODESYS Control Win V3 controller

Code Size (bytes)

Data Size (bytes)

Total (bytes)

Recipe definition with 100 INT variables

194406

79400

267352

Recipe definition with 200 INT variables

238318

121284

459344

Recipe definition with 300 INT variables

282230

163084

543856

Recipe definition with 100 BOOL variables

192742

69884

343168

Recipe definition with 200 BOOL variables

235446

101568

436872

Recipe definition with 300 BOOL variables

278146

133284

510072

Recipe definition with 100 string variables

203278

870084

1154000

Recipe definition with 200 string variables

255570

1709784

2973296

Recipe definition with 300 string variables

307886

2549484

2964112



Loading recipe values from the controller

You can apply recipe values on the controller to recipe definitions in the project, even if these definitions have been modified in the project.

Requirement: The Recipe management in the PLC is option is selected in the Recipe Manager.

  1. Create a recipe definition RecDef1 in the project, containing the variables PLC_PRG.ivar and PLC_PRG.bvar. Insert a recipe R1: value for PLC_PRG.ivar: 33; value for PLC_PRG.bvar: TRUE.

  2. Log in to the controller and download the application.

    The recipe file R1.RecDef1.txtrecipe is saved to the default directory of the controller ($PlcLogic$).

  3. Logout and add another variable PLC_PRG.dwvar to the recipe definition in the project.

  4. Edit the recipe definition file R1.RecDef1.txtrecipe on the device by changing the value for PLC_PRG.ivar from 33 to 34.

    Moreover, add another recipe R2 on the device. To do this, copy the R1.RecDef1.txtrecipe and rename it to R2.RecDef1.txtrecipe. Then edit this file and change the recipe values: PLC_PRG.ivar: 1, PLC_PRG.bvar: FALSE.

    Now two recipes R1 and R2 are available on the device. In the project, there is only R1, and it also contains other values than R1 on the device.

  5. Log in to the controller by means of an online change.

  6. Click Load Recipes from Device from the context menu.

    A dialog prompt notifies you that executing the command at the next login may trigger an online change, and that the recipes on the runtime system will overwrite the recipes of the current recipe definition.

  7. Confirm that you want to continue.

    A dialog prompt notifies you that the recipe for PLC_PRG.dwvar loaded on the device cannot yield a value from the controller.

  8. Confirm that you want to continue.

    The value of PLC_PRG.ivar in recipe R1 of the recipe definition in the project changes to 34. The recipe R2 with the values 1 and FALSE is also listed in the recipe definition now. PLC_PRG.dwvar remains in the recipe definition.